home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / dix / grabs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-14  |  10.7 KB  |  415 lines

  1. /* $XConsortium: grabs.c,v 5.7 89/12/11 15:43:47 keith Exp $ */
  2. /************************************************************
  3. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
  4. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  5.  
  6.                         All Rights Reserved
  7.  
  8. Permission to use, copy, modify, and distribute this software and its 
  9. documentation for any purpose and without fee is hereby granted, 
  10. provided that the above copyright notice appear in all copies and that
  11. both that copyright notice and this permission notice appear in 
  12. supporting documentation, and that the names of Digital or MIT not be
  13. used in advertising or publicity pertaining to distribution of the
  14. software without specific, written prior permission.  
  15. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  16. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  17. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  18. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  19. WHETHER IN AN action OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21. SOFTWARE.
  22.  
  23. ********************************************************/
  24.  
  25. #include "X.h"
  26. #include "misc.h"
  27. #define NEED_EVENTS
  28. #include "Xproto.h"
  29. #include "windowstr.h"
  30. #include "inputstr.h"
  31. #include "cursorstr.h"
  32.  
  33. extern InputInfo inputInfo;
  34.  
  35. #define BITMASK(i) (((Mask)1) << ((i) & 31))
  36. #define MASKIDX(i) ((i) >> 5)
  37. #define MASKWORD(buf, i) buf[MASKIDX(i)]
  38. #define BITSET(buf, i) MASKWORD(buf, i) |= BITMASK(i)
  39. #define BITCLEAR(buf, i) MASKWORD(buf, i) &= ~BITMASK(i)
  40. #define GETBIT(buf, i) (MASKWORD(buf, i) & BITMASK(i))
  41.  
  42. GrabPtr
  43. CreateGrab(client, device, window, eventMask, ownerEvents, keyboardMode,
  44.        pointerMode, modDevice, modifiers, type, keybut, confineTo, cursor)
  45.     int client;
  46.     DeviceIntPtr device;
  47.     WindowPtr window;
  48.     Mask eventMask;
  49.     Bool ownerEvents, keyboardMode, pointerMode;
  50.     DeviceIntPtr modDevice;
  51.     unsigned short modifiers;
  52.     int type;
  53.     KeyCode keybut;    /* key or button */
  54.     WindowPtr confineTo;
  55.     CursorPtr cursor;
  56. {
  57.     GrabPtr grab;
  58.  
  59.     grab = (GrabPtr)xalloc(sizeof(GrabRec));
  60.     if (!grab)
  61.     return (GrabPtr)NULL;
  62.     grab->resource = FakeClientID(client);
  63.     grab->device = device;
  64.     grab->coreGrab = ((device == inputInfo.keyboard) ||
  65.               (device == inputInfo.pointer));
  66.     grab->window = window;
  67.     grab->eventMask = eventMask;
  68.     grab->ownerEvents = ownerEvents;
  69.     grab->keyboardMode = keyboardMode;
  70.     grab->pointerMode = pointerMode;
  71.     grab->modifiersDetail.exact = modifiers;
  72.     grab->modifiersDetail.pMask = NULL;
  73.     grab->modifierDevice = modDevice;
  74.     grab->coreMods = ((modDevice == inputInfo.keyboard) ||
  75.               (modDevice == inputInfo.pointer));
  76.     grab->type = type;
  77.     grab->detail.exact = keybut;
  78.     grab->detail.pMask = NULL;
  79.     grab->confineTo = confineTo;
  80.     grab->cursor = cursor;
  81.     if (cursor)
  82.     cursor->refcnt++;
  83.     return grab;
  84.  
  85. }
  86.  
  87. static void
  88. FreeGrab(pGrab)
  89.     GrabPtr pGrab;
  90. {
  91.     if (pGrab->modifiersDetail.pMask != NULL)
  92.     xfree(pGrab->modifiersDetail.pMask);
  93.  
  94.     if (pGrab->detail.pMask != NULL)
  95.     xfree(pGrab->detail.pMask);
  96.  
  97.     if (pGrab->cursor)
  98.     FreeCursor(pGrab->cursor, (Cursor)0);
  99.  
  100.     xfree(pGrab);
  101. }
  102.  
  103. /*ARGSUSED*/
  104. int
  105. DeletePassiveGrab(pGrab, id)
  106.     GrabPtr pGrab;
  107.     XID   id;
  108. {
  109.     register GrabPtr g, prev;
  110.  
  111.     /* it is OK if the grab isn't found */
  112.     prev = 0;
  113.     for (g = (wPassiveGrabs (pGrab->window)); g; g = g->next)
  114.     {
  115.     if (pGrab == g)
  116.     {
  117.         if (prev)
  118.         prev->next = g->next;
  119.         else
  120.         if (!(pGrab->window->optional->passiveGrabs = g->next))
  121.             CheckWindowOptionalNeed (pGrab->window);
  122.         break;
  123.     }
  124.     prev = g;
  125.     }
  126.     FreeGrab(pGrab);
  127. }
  128.  
  129. static Mask *
  130. DeleteDetailFromMask(pDetailMask, detail)
  131.     Mask *pDetailMask;
  132.     unsigned short detail;
  133. {
  134.     register Mask *mask;
  135.     register int i;
  136.  
  137.     mask = (Mask *)xalloc(sizeof(Mask) * MasksPerDetailMask);
  138.     if (mask)
  139.     {
  140.     if (pDetailMask)
  141.         for (i = 0; i < MasksPerDetailMask; i++)
  142.         mask[i]= pDetailMask[i];
  143.     else
  144.         for (i = 0; i < MasksPerDetailMask; i++)
  145.         mask[i]= ~0L;
  146.     BITCLEAR(mask, detail);
  147.     }
  148.     return mask; 
  149. }
  150.  
  151. static Bool
  152. IsInGrabMask(firstDetail, secondDetail, exception)
  153.     DetailRec firstDetail, secondDetail;
  154.     unsigned short exception;
  155. {
  156.     if (firstDetail.exact == exception)
  157.     {
  158.     if (firstDetail.pMask == NULL)
  159.         return TRUE;
  160.     
  161.     /* (at present) never called with two non-null pMasks */
  162.     if (secondDetail.exact == exception)
  163.         return FALSE;
  164.  
  165.      if (GETBIT(firstDetail.pMask, secondDetail.exact))
  166.         return TRUE;
  167.     }
  168.     
  169.     return FALSE;
  170. }
  171.  
  172. static Bool 
  173. IdenticalExactDetails(firstExact, secondExact, exception)
  174.     unsigned short firstExact, secondExact, exception;
  175. {
  176.     if ((firstExact == exception) || (secondExact == exception))
  177.     return FALSE;
  178.    
  179.     if (firstExact == secondExact)
  180.     return TRUE;
  181.  
  182.     return FALSE;
  183. }
  184.  
  185. static Bool 
  186. DetailSupersedesSecond(firstDetail, secondDetail, exception)
  187.     DetailRec firstDetail, secondDetail;
  188.     unsigned short exception;
  189. {
  190.     if (IsInGrabMask(firstDetail, secondDetail, exception))
  191.     return TRUE;
  192.  
  193.     if (IdenticalExactDetails(firstDetail.exact, secondDetail.exact,
  194.                   exception))
  195.     return TRUE;
  196.   
  197.     return FALSE;
  198. }
  199.  
  200. static Bool
  201. GrabSupersedesSecond(pFirstGrab, pSecondGrab)
  202.     GrabPtr pFirstGrab, pSecondGrab;
  203. {
  204.     if (!DetailSupersedesSecond(pFirstGrab->modifiersDetail,
  205.                 pSecondGrab->modifiersDetail, 
  206.                 (unsigned short)AnyModifier))
  207.     return FALSE;
  208.  
  209.     if (DetailSupersedesSecond(pFirstGrab->detail,
  210.                    pSecondGrab->detail, (unsigned short)AnyKey))
  211.     return TRUE;
  212.  
  213.     return FALSE;
  214. }
  215.  
  216. Bool
  217. GrabMatchesSecond(pFirstGrab, pSecondGrab)
  218.     GrabPtr pFirstGrab, pSecondGrab;
  219. {
  220.     if ((pFirstGrab->device != pSecondGrab->device) ||
  221.     (pFirstGrab->modifierDevice != pSecondGrab->modifierDevice) ||
  222.     (pFirstGrab->type != pSecondGrab->type))
  223.     return FALSE;
  224.  
  225.     if (GrabSupersedesSecond(pFirstGrab, pSecondGrab) ||
  226.     GrabSupersedesSecond(pSecondGrab, pFirstGrab))
  227.     return TRUE;
  228.  
  229.     if (DetailSupersedesSecond(pSecondGrab->detail, pFirstGrab->detail,
  230.                    (unsigned short)AnyKey) 
  231.     && 
  232.     DetailSupersedesSecond(pFirstGrab->modifiersDetail,
  233.                    pSecondGrab->modifiersDetail,
  234.                    (unsigned short)AnyModifier))
  235.     return TRUE;
  236.  
  237.     if (DetailSupersedesSecond(pFirstGrab->detail, pSecondGrab->detail,
  238.                    (unsigned short)AnyKey)
  239.     && 
  240.     DetailSupersedesSecond(pSecondGrab->modifiersDetail,
  241.                    pFirstGrab->modifiersDetail,
  242.                    (unsigned short)AnyModifier))
  243.     return TRUE;
  244.  
  245.     return FALSE;
  246. }
  247.  
  248. int
  249. AddPassiveGrabToList(pGrab)
  250.     GrabPtr pGrab;
  251. {
  252.     GrabPtr grab;
  253.  
  254.     for (grab = wPassiveGrabs(pGrab->window); grab; grab = grab->next)
  255.     {
  256.     if (GrabMatchesSecond(pGrab, grab))
  257.     {
  258.         if (CLIENT_BITS(pGrab->resource) != CLIENT_BITS(grab->resource))
  259.         {
  260.         FreeGrab(pGrab);
  261.         return BadAccess;
  262.         }
  263.     }
  264.     }
  265.  
  266.     if (!pGrab->window->optional && !MakeWindowOptional (pGrab->window))
  267.     {
  268.     FreeGrab(pGrab);
  269.     return BadAlloc;
  270.     }
  271.     pGrab->next = pGrab->window->optional->passiveGrabs;
  272.     pGrab->window->optional->passiveGrabs = pGrab;
  273.     if (AddResource(pGrab->resource, RT_PASSIVEGRAB, (pointer)pGrab))
  274.     return Success;
  275.     return BadAlloc;
  276. }
  277.  
  278. /* the following is kinda complicated, because we need to be able to back out
  279.  * if any allocation fails
  280.  */
  281.  
  282. Bool
  283. DeletePassiveGrabFromList(pMinuendGrab)
  284.     GrabPtr pMinuendGrab;
  285. {
  286.     register GrabPtr grab;
  287.     GrabPtr *deletes, *adds;
  288.     Mask ***updates, **details;
  289.     int i, ndels, nadds, nups;
  290.     Bool ok;
  291.  
  292. #define UPDATE(mask,exact) \
  293.     if (!(details[nups] = DeleteDetailFromMask(mask, exact))) \
  294.       ok = FALSE; \
  295.     else \
  296.       updates[nups++] = &(mask)
  297.  
  298.     i = 0;
  299.     for (grab = wPassiveGrabs(pMinuendGrab->window); grab; grab = grab->next)
  300.     i++;
  301.     if (!i)
  302.     return TRUE;
  303.     deletes = (GrabPtr *)ALLOCATE_LOCAL(i * sizeof(GrabPtr));
  304.     adds = (GrabPtr *)ALLOCATE_LOCAL(i * sizeof(GrabPtr));
  305.     updates = (Mask ***)ALLOCATE_LOCAL(i * sizeof(Mask **));
  306.     details = (Mask **)ALLOCATE_LOCAL(i * sizeof(Mask *));
  307.     if (!deletes || !adds || !updates || !details)
  308.     {
  309.     if (details) DEALLOCATE_LOCAL(details);
  310.     if (updates) DEALLOCATE_LOCAL(updates);
  311.     if (adds) DEALLOCATE_LOCAL(adds);
  312.     if (deletes) DEALLOCATE_LOCAL(deletes);
  313.     return FALSE;
  314.     }
  315.     ndels = nadds = nups = 0;
  316.     ok = TRUE;
  317.     for (grab = wPassiveGrabs(pMinuendGrab->window);
  318.      grab && ok;
  319.      grab = grab->next)
  320.     {
  321.     if ((CLIENT_BITS(grab->resource) != CLIENT_BITS(pMinuendGrab->resource)) ||
  322.         !GrabMatchesSecond(grab, pMinuendGrab))
  323.         continue;
  324.     if (GrabSupersedesSecond(pMinuendGrab, grab))
  325.     {
  326.         deletes[ndels++] = grab;
  327.     }
  328.     else if ((grab->detail.exact == AnyKey)
  329.          && (grab->modifiersDetail.exact != AnyModifier))
  330.     {
  331.         UPDATE(grab->detail.pMask, pMinuendGrab->detail.exact);
  332.     }
  333.     else if ((grab->modifiersDetail.exact == AnyModifier) 
  334.          && (grab->detail.exact != AnyKey))
  335.     {
  336.         UPDATE(grab->modifiersDetail.pMask,
  337.            pMinuendGrab->modifiersDetail.exact);
  338.     }
  339.     else if ((pMinuendGrab->detail.exact != AnyKey)
  340.          && (pMinuendGrab->modifiersDetail.exact != AnyModifier))
  341.     {
  342.         GrabPtr pNewGrab;
  343.  
  344.         UPDATE(grab->detail.pMask, pMinuendGrab->detail.exact);
  345.  
  346.         pNewGrab = CreateGrab(CLIENT_ID(grab->resource), grab->device,
  347.                   grab->window, (Mask)grab->eventMask,
  348.                   (Bool)grab->ownerEvents,
  349.                   (Bool)grab->keyboardMode,
  350.                   (Bool)grab->pointerMode,
  351.                   grab->modifierDevice,
  352.                   AnyModifier, (int)grab->type,
  353.                   pMinuendGrab->detail.exact,
  354.                   grab->confineTo, grab->cursor);
  355.         if (!pNewGrab)
  356.         ok = FALSE;
  357.         else if (!(pNewGrab->modifiersDetail.pMask =
  358.                DeleteDetailFromMask(grab->modifiersDetail.pMask,
  359.                      pMinuendGrab->modifiersDetail.exact))
  360.              ||
  361.              (!pNewGrab->window->optional &&
  362.               !MakeWindowOptional(pNewGrab->window)))
  363.         {
  364.         FreeGrab(pNewGrab);
  365.         ok = FALSE;
  366.         }
  367.         else if (!AddResource(pNewGrab->resource, RT_PASSIVEGRAB,
  368.                   (pointer)pNewGrab))
  369.         ok = FALSE;
  370.         else
  371.         adds[nadds++] = pNewGrab;
  372.     }   
  373.     else if (pMinuendGrab->detail.exact == AnyKey)
  374.     {
  375.         UPDATE(grab->modifiersDetail.pMask,
  376.            pMinuendGrab->modifiersDetail.exact);
  377.     }
  378.     else
  379.     {
  380.         UPDATE(grab->detail.pMask, pMinuendGrab->detail.exact);
  381.     }
  382.     }
  383.  
  384.     if (!ok)
  385.     {
  386.     for (i = 0; i < nadds; i++)
  387.         FreeResource(adds[i]->resource, RT_NONE);
  388.     for (i = 0; i < nups; i++)
  389.         xfree(details[i]);
  390.     }
  391.     else
  392.     {
  393.     for (i = 0; i < ndels; i++)
  394.         FreeResource(deletes[i]->resource, RT_NONE);
  395.     for (i = 0; i < nadds; i++)
  396.     {
  397.         grab = adds[i];
  398.         grab->next = grab->window->optional->passiveGrabs;
  399.         grab->window->optional->passiveGrabs = grab;
  400.     }
  401.     for (i = 0; i < nups; i++)
  402.     {
  403.         xfree(*updates[i]);
  404.         *updates[i] = details[i];
  405.     }
  406.     }
  407.     DEALLOCATE_LOCAL(details);
  408.     DEALLOCATE_LOCAL(updates);
  409.     DEALLOCATE_LOCAL(adds);
  410.     DEALLOCATE_LOCAL(deletes);
  411.     return ok;
  412.  
  413. #undef UPDATE
  414. }
  415.